/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatroom.com.util;

import chatroom.com.chatEvent.Chat;
import java.io.*;
import javax.swing.JTextPane;

/**
 *
 * @author 李鹏飞
 */
public class FileForJiluUtil {

    
    public static void writeUserNameListByLine(String fileName,String userlist){
         try {
            createNewFile(fileName);
            // write string to file
            FileWriter writer = new FileWriter(fileName, true);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(userlist);
            bw.newLine();
            bw.flush();
            bw.close();
            writer.close();

        } catch (IOException e) {
        }
    }
    public static void createNewFile(String fileName) throws IOException {
        File myFilePath = new File(fileName);
        if (!myFilePath.exists()) {
            myFilePath.createNewFile();
        }
    }

    public static void appendJiluToFile(String fileName, String jilu) {
        try {
            createNewFile(fileName);
            // write string to file
            FileWriter writer = new FileWriter(fileName, true);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(jilu);
//            bw.newLine();
            bw.flush();
            bw.close();
            writer.close();

        } catch (IOException e) {
        }

    }
    private static int pageSize = 16;

    /**
     * anhang分页读取文件里面的内容
     *
     * @param pageNo
     * @param fileName
     */
    public static int readFileByPage(int pageNo, String fileName, JTextPane pane) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
            int index = 1;
            //一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //显示行号
                if (index % 2 != 0) {
                    line++;
                    if (line > (pageNo - 1) * pageSize && line <= pageNo * pageSize) {
                        System.out.println("line " + line + ": " + tempString);
                        Chat.insertIntoHistoryPane(pane, line + ":    " + tempString);
                    }
                }

                index = index + 1;
                if (line > pageNo * pageSize) {
                    break;
                }
            }
            reader.close();
        } catch (IOException e) {
        }
        return pageNo;
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     *
     * @param fileName 文件名
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            //一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static int getLine(String fileName) {
        File file = new File(fileName);
        int line = 1;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;

            //一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //显示行号
                System.out.println("line " + line + ": " + tempString);

                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return line;
    }

    public static String[] readFileByLine(String fileName) {
        File file = new File(fileName);
        String[] temp = new String[getLine(fileName)];
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            //一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                //显示行号
                System.out.println("line " + line + ": " + tempString);
                temp[line] = tempString;
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return temp;
    }

    /**
     * 以字符为单位读取文件，常用于读文本，数字等类型的文件
     *
     * @param fileName 文件名
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符为单位读取文件内容，一次读一个字节：");
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                //对于windows下，这两个字符在一起时，表示一个换行。
                //但如果这两个字符分开显示时，会换两次行。
                //因此，屏蔽掉，或者屏蔽。否则，将会多出很多空行。
                if (((char) tempchar) != ' ') {
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println("以字符为单位读取文件内容，一次读多个字节：");
            //一次读多个字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            //读入多个字符到字符数组中，charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
                //同样屏蔽掉不显示
                if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != ' ')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == ' ') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}
